#include <ESP8266WiFi.h>
const char* ssid = "Innovation";
const char* password = "09F#HSRs";
#include <PubSubClient.h>
const char* mqtt_server = "douzi.wiki";
const char* mqtt_username = "";
const char* mqtt_password = "";
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];

#include <BH1750.h>
#include <Wire.h>

BH1750 lightMeter;

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  String str;
  for (int i = 0; i < length; i++) {
    str += String((char)payload[i]);
    Serial.print((char)payload[i]);
  }
  Serial.println();
  Serial.print(str);
  Serial.println();
  Serial.print("-----");
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP826601Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
      Serial.println("connected");
      // Once connected, publish an announcement...

      // ... and resubscribe
      client.subscribe("/home/r/espdht11");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  
  }
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  //声音
  pinMode(12, INPUT);
  Wire.begin(4,5);//5--SCL  4--SDA
  lightMeter.begin();
  Serial.println(F("BH1750 Test"));
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}
void loop() {
  // put your main code here, to run repeatedly:
  if (!client.connected()) {
    reconnect();
  }else if(client.connected()){
    delay(5000);
    //光照度数据上报
  float lux = lightMeter.readLightLevel();
  String lux_string = String(lux,0);
//    string to char add .c_str()
    client.publish("/intelligentCurtain/illuminance",lux_string.c_str());
      Serial.print("Light: ");
  Serial.print(lux_string);
  Serial.println(" lx");
  }
  //紫外线数据上报
  int sensorValue;
  long  sum=0;
  for(int i=0;i<1024;i++)
   {  
      sensorValue=analogRead(A0);
      sum=sensorValue+sum;
      delay(2);
   }   
 sum = sum >> 10;
 Serial.print("The voltage value:");
 Serial.print(sum*4980.0/1023.0);
 Serial.println("mV");
   String ultraviolet_string = String(sum*4980.0/1023.0,0);
//    string to char add .c_str()
    client.publish("/intelligentCurtain/ultraviolet",ultraviolet_string.c_str());
    //声音数据上报
      if (digitalRead(12) == LOW) {
      delay(10);
    if (digitalRead(12) == LOW) {
            client.publish("/intelligentCurtain/volume","1");
      Serial.println("aLoud");
    }
  }
  if (digitalRead(12) == HIGH) {   
    delay(10);
    if (digitalRead(12) == HIGH) {
            client.publish("/intelligentCurtain/volume","0");
      Serial.println("NotaLoud");
    }
  }
  client.loop();
}
